package com.xinzhi.controller;

import com.xinzhi.pojo.LoanApplication;
import com.xinzhi.service.impl.LoanApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/loan/application")
@Validated // 启用方法级别的验证
public class LoanApplicationController {

    @Autowired
    private LoanApplicationService loanApplicationService;

    /**
     * POST 请求，创建贷款申请
     *
     * @param loanNumber 贷款号，只能包含字母和数字
     * @param userId 用户ID，必须大于0
     * @param loanType 贷款类型，不能为空
     * @param loanRange 贷款范围，不能为空
     * @param guarantorId 担保人ID，可选
     * @param loanAmount 贷款金额，必须大于0
     * @param applicationDate 申请日期，格式为yyyy-MM-dd
     * @param repaymentMethod 还款方式，不能为空
     * @param monthlyRepaymentAmount 月还款金额，必须大于0
     * @param creditInvestigator 信用调查员，不能为空
     * @return 返回创建贷款申请的结果消息，例如"Loan application created successfully!" 或 "Failed to create loan application: 错误消息"
     */
    @PostMapping("/create")
    public String createLoanApplication(
            @RequestParam @Pattern(regexp = "^[A-Za-z0-9]+$", message = "贷款号只能包含字母和数字") String loanNumber,
            @RequestParam @Min(value = 1, message = "用户ID必须大于0") Integer userId,
            @RequestParam @NotBlank(message = "贷款类型不能为空") String loanType,
            @RequestParam @NotBlank(message = "贷款范围不能为空") String loanRange,
            @RequestParam(required = false) Integer guarantorId,
            @RequestParam @DecimalMin(value = "0.01", message = "贷款金额必须大于0") BigDecimal loanAmount,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date applicationDate,
            @RequestParam @NotBlank(message = "还款方式不能为空") String repaymentMethod,
            @RequestParam @DecimalMin(value = "0.01", message = "月还款金额必须大于0") BigDecimal monthlyRepaymentAmount,
            @RequestParam @NotBlank(message = "信用调查员不能为空") String creditInvestigator) {

        try {
            LoanApplication loanApplication = new LoanApplication();
            loanApplication.setApplicationNumber(loanNumber);
            loanApplication.setUserId(userId);
            loanApplication.setLoanType(loanType);
            loanApplication.setLoanScope(loanRange);
            loanApplication.setGuarantorId(guarantorId);
            loanApplication.setLoanAmount(loanAmount);
            loanApplication.setApplicationDate(applicationDate);
            loanApplication.setRepaymentMethod(repaymentMethod);
            loanApplication.setMonthlyRepayment(monthlyRepaymentAmount);
            loanApplication.setInvestigatorId(creditInvestigator);

            loanApplicationService.createLoanApplication(loanApplication);
            return "贷款创建成功辛苦啦" +
                    "!";
        } catch (Exception e) {
            e.printStackTrace();
            return "贷款申请创建失败仔细一点: " + e.getMessage();
        }
    }

    /**
     * GET 请求，获取所有贷款申请
     *
     * @return 返回所有贷款申请的列表
     */
    @GetMapping("/all")
    public List<LoanApplication> getAllLoanApplications() {
        return loanApplicationService.getAllLoanApplications();
    }

    /**
     * GET 请求，根据 ID 获取贷款申请
     *
     * @param id 贷款申请的ID
     * @return 返回对应ID的贷款申请信息
     */
    @GetMapping("/{id}")
    public LoanApplication getLoanApplicationById(@PathVariable Integer id) {
        return loanApplicationService.getLoanApplicationById(id);
    }
}
